在 C 層級,Ruby 物件是以分離的結構來表示 資料與行為 透過指標層級結構。其中 klass 指標將一個實例連結至其「關聯類別」(方法儲存的位置),而 super 指標則將類別連結至其祖先,形成一種結構圖表,供 Ruby 虛擬機用以解析繼承關係。
1. klass 指標與旗標
每個 Ruby 物件都包含一個標頭,內含一個 klass 指標與一個位元遮罩 旗標。這些旗標可識別物件的內部狀態,例如以 'V' 旗標標記自動建立的「虛擬類別」,以處理單例方法的設定。
2. 解耦狀態(圖 24.1)
像 lucille 會儲存其獨特的 實例變數,但本身並無方法。其 klass 指標會引導虛擬機至 Guitar 類別物件的方法表格。
3. 繼承鏈
當 lucille.play() 被呼叫時,Ruby 會追隨 klass 指標。若方法在 Guitar中找不到,就會追隨 super 指標至 Object,從此取得對 clone 或 dup的存取權。
main.py
TERMINALbash — 80x24
> Ready. Click "Run" to execute.
>
QUESTION 1
Where are an object's methods (like
def play()) actually stored?Inside the instance structure itself alongside variables.
In the method table of the class pointed to by 'klass'.
In a global Ruby registry.
Inside the 'super' pointer destination only.
✅ Correct!
Correct! Instances hold state (variables), while classes hold logic (methods).❌ Incorrect
Recall that Ruby decouples state from logic to save memory; methods reside in the class object.QUESTION 2
What does the 'V' flag represent in a class's internal flags?
Version: The Ruby version used to create it.
Virtual: It is an automatically created singleton class.
Value: The class represents a primitive type.
Validated: The class has passed security checks.
✅ Correct!
Yes! Virtual classes are inserted into the chain to hold singleton behavior.❌ Incorrect
The 'V' flag marks virtual (singleton) classes that aren't visible in the standard class hierarchy.QUESTION 3
What is the primary function of the
super pointer in a class object?To point to the object's instance variables.
To point to the class's own class (metaclass).
To point to the parent class for method lookup.
To link an instance back to its constructor.
✅ Correct!
Exactly. The super pointer creates the 'inheritance chain' used for method resolution.❌ Incorrect
The super pointer handles inheritance, linking a class to its ancestor (e.g., Guitar to Object).QUESTION 4
If you define
def Guitar.strings, where is that method stored?In the Guitar class method table.
In a virtual class inserted into Guitar's own hierarchy.
In the Object class table.
Directly inside the 'lucille' instance.
✅ Correct!
Defining a class-level method creates a singleton/virtual class for the Guitar class object itself.❌ Incorrect
Class methods are essentially singleton methods on the class object, requiring a virtual class.QUESTION 5
Does calling
lucille.clone use the klass or super pointer first?It follows
klass to Guitar, then super to Object.It bypasses
klass and goes straight to Object.It searches the local
iv_tbl first.It follows
super from the instance.✅ Correct!
Ruby always starts at the immediate class (klass) and climbs the super chain until the method is found.❌ Incorrect
Lookup always starts at the 'klass' and traverses upwards via 'super' pointers.Internal Lookup Challenge
Analyzing the RObject and RClass structures
A developer creates a Guitar instance named 'lucille'. They then define a singleton method `def lucille.tune; end`. You are tasked with mapping the pointer journey during the call `lucille.tune`.
Q
Which pointer does Ruby follow first when `lucille.tune` is invoked?
Solution:
Ruby follows the `klass` pointer of the `lucille` instance. Because a singleton method was defined, this `klass` pointer now targets a hidden 'Virtual Class' containing the `tune` method.
Ruby follows the `klass` pointer of the `lucille` instance. Because a singleton method was defined, this `klass` pointer now targets a hidden 'Virtual Class' containing the `tune` method.
Q
Explain why `lucille.class` still returns 'Guitar' even though its `klass` pointer targets a virtual class.
Solution:
Ruby's `class` method is designed to skip over virtual classes (those marked with the 'V' flag) to return the first logical, non-virtual class in the inheritance chain.
Ruby's `class` method is designed to skip over virtual classes (those marked with the 'V' flag) to return the first logical, non-virtual class in the inheritance chain.